home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / By the Book / Mac C Primer V2 / 4.4 - GWorld / GWorld.c next >
C/C++ Source or Header  |  1991-08-21  |  5KB  |  206 lines

  1. /************************************************************/
  2. /*                                                            */
  3. /*    GWorld Code from Chapter Four of                        */
  4. /*                                                            */
  5. /*        *** The Macintosh Programming Primer ***            */
  6. /*                                                            */
  7. /*    Copyright 1990, Dave Mark                                */
  8. /*                                                            */
  9. /*    This program demonstrates specific Mac programming        */
  10. /*    techniques.                                                */
  11. /*                                                            */
  12. /************************************************************/
  13.  
  14. #include "Picker.h"
  15. #include "QDOffscreen.h"
  16.  
  17. #define BASE_RES_ID            400
  18. #define NIL_POINTER            0L
  19. #define NIL_STRING            "\p"
  20. #define VISIBLE                TRUE
  21. #define NO_GOAWAY            FALSE
  22. #define MOVE_TO_FRONT        (WindowPtr)-1L
  23. #define REMOVE_ALL_EVENTS    0
  24.  
  25. #define MAX_PIXEL_DEPTH        32
  26. #define WORLD_WIDTH            100
  27. #define WORLD_HEIGHT        100
  28. #define NO_FLAGS            0L
  29.  
  30. #define QD32TRAP            0xAB03
  31. #define UNIMPL_TRAP            0xA89F
  32.  
  33.  
  34. Boolean         Is32Bit();
  35. GWorldPtr        MakeGWorld();
  36. WindowPtr        CreateColorWindow();
  37.  
  38.  
  39. main()
  40. {
  41.     WindowPtr        window;
  42.     GWorldPtr        world;
  43.     Rect            worldBounds, windowRect, destRect;
  44.     
  45.     ToolBoxInit();
  46.     
  47.     if ( ! Is32Bit() )
  48.         DoAlert( "\pThis machine does not support 32-Bit QuickDraw!" );
  49.     else
  50.     {
  51.         SetRect( &worldBounds, 0, 0, WORLD_HEIGHT, WORLD_WIDTH );
  52.         world = MakeGWorld( &worldBounds );
  53.         window = CreateColorWindow();
  54.         
  55.         SetRect( &destRect, 0, 0, 4 * WORLD_WIDTH, 4 * WORLD_HEIGHT );
  56.         CopyWorldBits( world, window, &destRect );
  57.         
  58.         SetRect( &destRect, 0, 0, 2 * WORLD_WIDTH, 2 * WORLD_HEIGHT );
  59.         CopyWorldBits( world, window, &destRect );
  60.         
  61.         SetRect( &destRect, 0, 0, WORLD_WIDTH, WORLD_HEIGHT );
  62.         CopyWorldBits( world, window, &destRect );
  63.         
  64.         SetRect( &destRect, 0, 0, WORLD_WIDTH / 2, WORLD_HEIGHT / 2 );
  65.         CopyWorldBits( world, window, &destRect );
  66.         
  67.         SetRect( &destRect, 0, 0, WORLD_WIDTH / 4, WORLD_HEIGHT / 4 );
  68.         CopyWorldBits( world, window, &destRect );
  69.         
  70.         while ( ! Button() ) ;
  71.     }
  72. }
  73.  
  74.  
  75. /*********************************** ToolBoxInit */
  76.  
  77. ToolBoxInit()
  78. {
  79.     InitGraf( &thePort );
  80.     InitFonts();
  81.     FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
  82.     InitWindows();
  83.     InitMenus();
  84.     TEInit();
  85.     InitDialogs( NIL_POINTER );
  86.     InitCursor();
  87. }
  88.  
  89.  
  90. /*********************************** CreateColorWindow */
  91.  
  92. WindowPtr    CreateColorWindow()
  93. {
  94.     WindowPtr    cWindow;
  95.     Rect        r;
  96.     
  97.     SetRect( &r, 10, 40, 10 + (4 * WORLD_WIDTH),
  98.                 40 + (4 * WORLD_HEIGHT) );
  99.                         
  100.     cWindow = NewCWindow( NIL_POINTER, &r, "\pColor Test",
  101.             VISIBLE, noGrowDocProc, MOVE_TO_FRONT,
  102.             NO_GOAWAY, NIL_POINTER );
  103.     
  104.     SetPort( cWindow );
  105.     
  106.     return( cWindow );
  107. }
  108.  
  109.  
  110. /*********************************** MakeGWorld */
  111.  
  112. GWorldPtr MakeGWorld( boundsPtr )
  113. Rect    *boundsPtr;
  114. {
  115.     GDHandle    oldGD;
  116.     GWorldPtr    oldGW, newWorld;
  117.     HSVColor    hsvColor;
  118.     RGBColor    rgbColor;
  119.     long        i;
  120.     Rect        r;
  121.     QDErr        errorCode;
  122.     
  123.     GetGWorld( &oldGW, &oldGD );
  124.     
  125.     errorCode = NewGWorld( &newWorld, MAX_PIXEL_DEPTH,
  126.                             boundsPtr, NIL_POINTER,
  127.                             NIL_POINTER, NO_FLAGS );
  128.     if ( errorCode != noErr )
  129.     {
  130.         DoAlert( "\pMy call to NewGWorld died!  Bye..." );
  131.         ExitToShell();
  132.     }
  133.         
  134.     LockPixels( newWorld->portPixMap );
  135.     SetGWorld( newWorld, NIL_POINTER );
  136.     
  137.     hsvColor.value = 65535;
  138.     hsvColor.saturation = 65535;
  139.  
  140.     for( i=boundsPtr->left; i<=boundsPtr->right; i++ )
  141.     {
  142.         hsvColor.hue = i * 65535 / ( boundsPtr->right - 1 );
  143.         HSV2RGB( &hsvColor, &rgbColor );
  144.         RGBForeColor( &rgbColor );
  145.         MoveTo( i, boundsPtr->bottom / 2 );
  146.         LineTo( i, boundsPtr->bottom );
  147.         
  148.         rgbColor.red = i * 65535 / ( boundsPtr->right - 1 );
  149.         rgbColor.green = rgbColor.red;
  150.         rgbColor.blue = rgbColor.red;
  151.                      
  152.         RGBForeColor( &rgbColor );
  153.         MoveTo( i, 0 );
  154.         LineTo( i, boundsPtr->bottom / 2 );
  155.     }
  156.     
  157.     SetGWorld(oldGW,oldGD);
  158.     UnlockPixels( newWorld->portPixMap );
  159.     
  160.     return( newWorld );
  161. }
  162.  
  163.  
  164. /*********************************** CopyWorldBits */
  165.  
  166. CopyWorldBits( world, window, destRectPtr )
  167. GWorldPtr    world;
  168. WindowPtr    window;
  169. Rect        *destRectPtr;
  170. {
  171.     RGBColor    rgbBlack;
  172.     
  173.     rgbBlack.red = rgbBlack.green = rgbBlack.blue = 0;
  174.     RGBForeColor( &rgbBlack );
  175.     
  176.     LockPixels( world->portPixMap );
  177.     CopyBits( &world->portPixMap, &thePort->portBits,
  178.         &world->portRect, destRectPtr, ditherCopy, 0 );
  179.     UnlockPixels( world->portPixMap );
  180. }
  181.  
  182.  
  183. /******************************** Is32Bit *********/
  184.  
  185. Boolean Is32Bit()
  186. {
  187.     SysEnvRec    mySE;
  188.     
  189.     SysEnvirons( 1, &mySE );
  190.     
  191.     if ( ! mySE.hasColorQD )
  192.         return( FALSE );
  193.     
  194.     return( NGetTrapAddress( QD32TRAP, ToolTrap ) !=
  195.         NGetTrapAddress( UNIMPL_TRAP, ToolTrap ) );
  196. }
  197.  
  198.  
  199. /*********************************** DoAlert */
  200.  
  201. DoAlert( s )
  202. Str255        s;
  203. {
  204.     ParamText( s, NIL_STRING, NIL_STRING, NIL_STRING );
  205.     NoteAlert( BASE_RES_ID, NIL_POINTER );
  206. }